package com.cloudbroker.bcs.common.util;

import java.util.*;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public abstract class JSONUtil {
    
    public static final String EMPTY_JSON_OBJECT_STRING = "{}";
    
    public static JSONObject merge(JSONObject... jsonObjects) {
        int length = 0;
        if (null == jsonObjects || 0 == (length = jsonObjects.length)) {
            return new JSONObject();
        }
        JSONObject base = jsonObjects[0];
        for (int i = 1; i < length; i++) {
            JSONObject other = jsonObjects[i];
            if (null == other) {
                continue;
            }
            for (Entry<String, Object> entry : other.entrySet()) {
                base.put(entry.getKey(), entry.getValue());
            }
        }
        return base;
    }
    
    public static JSONObject merge(JSONObject base, String key, Object value) {
        if (null == base) {
            base = new JSONObject();
        }
        if (null != key) {
            base.put(key, value);
        }
        return base;
    }
    
    public static JSONObject merge(JSONObject base, Map<String, Object> map) {
        if (null == base) {
            base = new JSONObject();
        }
        if (null != map && !map.isEmpty()) {
            for (Entry<String, Object> entry : map.entrySet()) {
                base.put(entry.getKey(), entry.getValue());
            }
        }
        return base;
    }
    
    public static JSONObject merge(JSONObject base, Properties prop) {
        if (null == base) {
            base = new JSONObject();
        }
        if (null != prop && !prop.isEmpty()) {
            for (Entry<Object, Object> entry : prop.entrySet()) {
                base.put((String) entry.getKey(), entry.getValue());
            }
        }
        return base;
    }
    
    public static boolean equals(JSONObject o, JSONObject ao) {
        if (o == ao) {
            return true;
        }
        if (null == o) {
            return null == ao;
        }
        if (null == ao) {
            return false;
        }
        return o.equals(ao);
    }
    
    public static boolean equals(JSONArray o, JSONArray ao) {
        if (o == ao) {
            return true;
        }
        if (null == o) {
            return null == ao;
        }
        if (null == ao) {
            return false;
        }
        return o.equals(ao);
    }
    
    public static boolean equals(JSONObject o, JSONObject ao, String[] keys) {
        if (o == ao) {
            return true;
        }
        if (null == o) {
            return null == ao;
        }
        if (null == ao) {
            return false;
        }
        if (null == keys || 0 == keys.length) {
            return true;
        }
        for (String key : keys) {
            Object ov = o.get(key);
            Object aov = ao.get(key);
            if (null == ov) {
                if (null != aov) {
                    return false;
                }
            } else {
                if (null == aov) {
                    return false;
                } else {
                    if (!ov.equals(aov)) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
    
    public static JSONObject clone(JSONObject o) {
        if (null == o) {
            return null;
        }
        return (JSONObject) o.clone();
    }
    
    public static JSONArray clone(JSONArray o) {
        if (null == o) {
            return null;
        }
        return (JSONArray) o.clone();
    }
    
    public static <F, T> T trans(F from, Class<T> toClass) {
        String jsonString = JSON.toJSONString(from);
        return JSON.parseObject(jsonString, toClass);
    }
    
    public static <T> T trans(Map<String, Object> from, Class<T> toClass) {
        JSONObject json = new JSONObject(from);
        return JSON.toJavaObject(json, toClass);
    }
    
    public static <T> List<T> trans(List<Map<String, Object>> fromList, Class<T> toElementClass) {
        int size = fromList.size();
        List<T> resultList = new ArrayList<T>(size);
        for (int i = 0; i < size; i++) {
            Map<String, Object> from = fromList.get(i);
            T element = null;
            if (null != from) {
                element = trans(from, toElementClass);
            }
            resultList.add(element);
        }
        return resultList;
    }
    
    public static JSONObject toJsonObject(Object obj) {
        try {
            if(obj instanceof String){
                return JSONObject.parseObject((String) obj);
            }
            return JSONObject.parseObject(JSONObject.toJSONString(obj));
        } catch (Exception e) {
            return new JSONObject();
        }
    }
    
    public static JSONArray toJsonArray(Object obj) {
        try {
            return JSONArray.parseArray(ValueUtil.getString(obj, ""));
        } catch (Exception e) {
            return new JSONArray();
        }
    }
    
    public static void main(String[] args) {
        String str = "[{\"type\":\"text\",\"body\":\"11\"},{\"type\":\"img\",\"body\":[]},{\"type\":\"audio\",\"body\":[]},{\"type\":\"article\",\"body\":[]},{\"type\":\"msg\",\"body\":[]},{\"type\":\"stock\",\"body\":[]}]";
        System.out.println(bodyNotEmpty(str));
    }
    
    public static String bodyNotEmpty(String content) {
        JSONArray jsonArray = JSONArray.parseArray(content);
        Object[] objects = jsonArray.toArray();
        int idx = 0;
        for (Object obj : objects) {
            JSONObject jo = JSONUtil.toJsonObject(obj);
            String type = ValueUtil.getString(jo.get("type").toString(), "");
            if (type.equals("text")) {
                jo.put("body", ValueUtil.getString(jo.get("body"), " "));
            } else if (type.equals("article")) {
                JSONArray ja = JSONUtil.toJsonArray(jo.get("body"));
                int articleIdx = 0;
                for (Object articleObj : ja.toArray()) {
                    JSONObject articleJo = JSONUtil.toJsonObject(articleObj);
                    ja.remove(articleIdx);
                    ja.add(articleIdx, JSONUtil.putObject(articleJo, " ", "source", "title", "content", "abst"));
                    articleIdx++;
                }
                if (ja.toArray().length == 0) {
                    ja.add(JSONUtil.putObject(null, " ", "source", "title", "content", "abst"));
                }
                jo.remove("body");
                jo.put("body", ja);
            }
            jsonArray.remove(idx);
            jsonArray.add(idx, jo);
            idx++;
        }
        return jsonArray.toString();
    }
    
    public static JSONObject putObject(JSONObject jo, String value, String... keys) {
        if (null == jo) {
            jo = new JSONObject();
        }
        if (null != keys) {
            for (String key : keys) {
                jo.put(key, ValueUtil.getString(jo.get(key), value));
            }
        }
        return jo;
    }
}
